'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
  //event为客户端上传的参数
  let {
    _id,
    flag,
    data
  } = event, result

  if (flag == 1) {
    result = await db.collection("zhimuta-cash-read")
      .where({
        cashInfoID: _id
      })
      .get()
  }
  if (flag == 2) {
    let res = await db.collection("zhimuta-cash-read")
      .where({
        cashInfoID: _id
      })
      .count()
    if (res.total > 0) {
      result = await db.collection("zhimuta-cash-read")
        .where({
          cashInfoID: _id
        })
        .update({
          data
        })
    } else {
      result = await db.collection("zhimuta-cash-read")
        .add({
          data,
          cashInfoID: _id
        })
    }
  }

  if (flag == 3) {
    result = await db.collection("zhimuta-cash-read")
      .where({
        cashInfoID: _id
      })
      .remove()
  }

  //返回数据给客户端
  return result
};